package fr.isen.smssender.sms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import fr.isen.smssender.R;
import fr.isen.smssender.database.DBManager;


public class SmsParameterActivity extends Activity{

    private Sms current;
    private int idSms;
    private EditText editText;
    private DBManager myDBManager;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_parameter);
        Bundle bundle = getIntent().getExtras();
        idSms = bundle.getInt("id");

        myDBManager = new DBManager(this);

        current = (idSms == -1)? new Sms("") : myDBManager.getSms(idSms);
        editText = (EditText) findViewById(R.id.editTextSms);
        if (idSms != -1) editText.setText(current.getMessage());
        
        final Button button = (Button) findViewById(R.id.buttonRegister);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String changeText = editText.getText().toString();
                current.setMessage(changeText);
                myDBManager.setSms(current);
                returnMainActivity(v);
            }
        });

        final Button button2 = (Button) findViewById(R.id.buttonDelete);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myDBManager.removeSms(idSms);
                returnMainActivity(v);
            }
        });
    }

    public  void returnMainActivity(View v)
    {
        Intent intent = new Intent(this, SmsListActivity.class);
        startActivity(intent);
    }
}
